import {
  IM_API_URL
} from '../../common/common';

function getConversions() {
  return new Promise((resolve) => {
    wx.request({
      url: IM_API_URL + "/im-service/im/conversions?user_id=" + getApp().globalData.userInfo.userId,
      method: "GET",
      header: {
        'content-type': 'application/json',
        'Authorization': getApp().globalData.userInfo.token
      },
      success(res) {
        if (res.statusCode == 200 || res.statusCode == 201) {
          resolve(res.data);
        }
      },
      fail(err) {
        reject(err);
      }
    })
  });
}

function getMessageList(data) {
  return new Promise((resolve) => {
    wx.request({
      url: IM_API_URL + "/im-service/im/messages?group_id=" + data,
      method: "GET",
      header: {
        'content-type': 'application/json',
        'Authorization': getApp().globalData.userInfo.token
      },
      success(res) {
        if (res.statusCode == 200 || res.statusCode == 201) {
          resolve(res.data);
        }
      },
      fail(err) {
        reject(err);
      }
    })
  });
}

function createGroup(data) {
  return new Promise((resolve) => {
    wx.request({
      url: IM_API_URL + "/im-service/im/create/group",
      method: "POST",
      data: data,
      header: {
        'content-type': 'application/json',
        'Authorization': getApp().globalData.userInfo.token
      },
      success(res) {
        if (res.statusCode == 200 || res.statusCode == 201) {
          resolve(res.data);
        }
      },
      fail(err) {
        reject(err);
      }
    })
  });
}

module.exports = {
  getConversions,
  getMessageList,
  createGroup
};